<template>
  <div class="login_container">
    <div class="login_box">
      <!--头像区域  -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单登录区域 -->
      <!-- 不能使用v-model指令 坑：使用后无法进行数据的验证-->
      <el-form  ref="loginFormRef"   label-width="0px" :rules="loginFormRules" :model="loginForm" class="login_form" >
        <el-form-item prop="username" >
          <el-input prefix-icon="iconfont icon-yonghu" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="iconfont icon-mima" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item prop="username" >
          <el-input prefix-icon="iconfont icon-yonghu" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="iconfont icon-mima" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="register">注册</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginForm:{
        username:"",
        password:""
      },
      loginFormRules:{
        username:[
           { required: true, message: '请输入登录名称', trigger: 'blur' },
           { min: 3, max: 13, message: '长度在 3 到 13 个字符', trigger: 'blur' }
        ],
        password:[
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 3, max: 11, message: '长度在 3 到 11 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    login(){
      //调用validata()实现表单的预验证 传一个回调函数回调函数的参数布尔值
      this.$refs.loginFormRef.validate(async (valid)=>{
        //console.log(valid)  valid是布尔值
        if(!valid) return;
        const {data:res} =await this.$http.post('login',this.loginForm)
        if(res.meta.status !==200) return this.$message.error('登录失败') 
        this.$message.success('登录成功')
      })
    },
    register(){
      
    },
    resetLoginForm(){
      this.$refs.loginFormRef.resetFields()//通过ref获取表单实例 调用element-ui表单自带的resetFields()方法重置表单
    },
  },
  }
</script>
<style lang="less" scoped>
.login_container {
  background-color: #bdb76b;
  height: 100%
}
.login_box {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%)
}
.avatar_box {
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  position: absolute;
  padding: 10px;
  box-shadow: 0, 0, 10px, #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: #eee
  }
}
.login_form {
    position: absolute;
    bottom:0;
    width: 100%;
    padding-left: 20px;
    padding-right: 20px;
    -webkit-box-sizing: border-box;
    box-sizing:border-box;
  }
.btns {
    display: flex;
    justify-content: flex-end
  }
</style>